package util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import annotation.controllerAnnotation;
import annotation.forwardAnnotation;
import bean.XmlBean;

public class AnnotationToBean {
	public static Map<String, XmlBean> xmlToBean(String tomcatPath,String packagePath) throws Exception {

		Map<String, XmlBean> pmap = new HashMap<String, XmlBean>();

		String actionpath = packagePath;

			for (String fileName : new File(tomcatPath + "\\WEB-INF\\classes\\"+ actionpath).list()) {

				String actionClass = actionpath + "." + fileName.split("\\.")[0];

				Class clazz = Class.forName(actionClass);

				if (clazz.isAnnotationPresent(controllerAnnotation.class)) {

					XmlBean xmlBean = new XmlBean();

					Map<String, String> forwardsMap = new HashMap<String, String>();

					controllerAnnotation actionAnnotation = (controllerAnnotation) clazz.getAnnotation(controllerAnnotation.class);

					String path = actionAnnotation.path();

					String formClass = actionAnnotation.formClass();

					xmlBean.setPath(path);

					xmlBean.setFormClass(formClass);

					xmlBean.setActionClass(actionClass);

					forwardAnnotation[] forwardAnnotations = actionAnnotation.forward();

					for (forwardAnnotation forward : forwardAnnotations) {

						forwardsMap.put(forward.name(), forward.url());

					}

					xmlBean.setActionForward(forwardsMap);

					pmap.put(path, xmlBean);

				}



			}




		return pmap;

	}



}